package com.itheima.tlias.service.impl;

import com.itheima.tlias.pojo.dto.SelectDeptDTO;
import com.itheima.tlias.pojo.entity.Dept;
import com.itheima.tlias.mapper.DeptMapper;
import com.itheima.tlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DeptServiceimpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<SelectDeptDTO> selectAll() {
        return deptMapper.selectAll();
    }

    @Override
    public int insertDept(Dept dept) {
        return deptMapper.insertDept(dept);
    }

    @Override
    public int deleteById(Integer id) {
        return deptMapper.deleteById(id);
    }

    @Override
    public SelectDeptDTO selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public int updateDept(SelectDeptDTO dept) {
        dept.setUpdateTime(LocalDateTime.now());
        //校验部门名称是否重复
        Dept d = deptMapper.selectDeptByName(dept.getName());
        //对比id判断是否是当前部门————不是则不可修改 是则修改
        if (Objects.nonNull(d) && !Objects.equals(d.getId(), dept.getId())) {
            //不是当前部门
            return 0;
        }
        return deptMapper.updateDept(dept);
    }
    //用xml写 部门的批量删除以及批量新增
}
